﻿Imports AutoCAD

Public Class CadPlateServiceImpl
    Implements ICadPlateService
    Private cadEntityDataService As ICadEntityDataService = New CadEntityDataServiceImpl
    Private cadApplicationService As ICadApplicationService = New CadApplicationServiceImpl
    Private cadBlockReferenceService As ICadBlockReferenceService = New CadBlockReferenceServiceImpl

    Public Function DrawPlate(ByRef plate As PlateEntity) As AcadBlockReference Implements ICadPlateService.DrawPlate
        Dim doc As AcadDocument = cadApplicationService.GetActiveDocument
        Dim basePlate As AcadBlock = BuildBasePlateBlock(doc)
        Dim br As AcadBlockReference = doc.ModelSpace.InsertBlock(New Double() {0, 0, 0}, basePlate.Name, plate.sx, plate.sy, plate.sz, 0)
        cadBlockReferenceService.Rotation(br, New Double() {plate.rx, plate.ry, plate.rz})
        br.InsertionPoint = New Double() {plate.px, plate.py, plate.pz}
        br.Update()
        Return br
    End Function


    Public Function SolidToPlate(ByRef ent As AcadEntity) As AcadBlockReference Implements ICadPlateService.SolidToPlate
        If Not cadEntityDataService.IsSolid(ent) Then
            Debug.Print("三维实体转板件失败, 非实体图元")
            Return Nothing
        End If

        ' 获取bbox
        Dim minp, maxp
        ent.GetBoundingBox(minp, maxp)

        ' 获取文档
        Dim doc As AcadDocument = ent.Document

        ' 创建板件块对象
        ent.Move(minp, New Double() {0, 0, 0})
        Dim b As AcadBlock = doc.Blocks.Add(New Double() {0, 0, 0}, ent.ObjectID)
        doc.CopyObjects(New AcadEntity() {ent}, b)
        ent.Delete()

        ' 插入块
        Dim plate As AcadBlockReference = doc.ModelSpace.InsertBlock(minp, b.Name, 1, 1, 1, 0)
        plate.Layer = "板件"
        Return plate

    End Function




    Public Function DrawPlate(ByRef ms As IAcadBlock, ByVal sx As Double, ByVal sy As Double, ByVal sz As Double) As AcadBlockReference Implements ICadPlateService.DrawPlate
        Dim basePlate As AcadBlock = BuildBasePlateBlock(ms.Document)
        Dim plate As AcadBlockReference = ms.Document.ModelSpace.InsertBlock(New Double() {0, 0, 0}, basePlate.Name, sx, sy, sz, 0)
        Return plate
    End Function



    Public Function BuildBasePlateBlock(ByRef doc As AcadDocument) As AcadBlock Implements ICadPlateService.BuildBasePlateBlock
        For Each b As AcadBlock In doc.Blocks
            If b.Name = "工件" Then Return b
        Next b

        ' 没有名为【工件】的块则构建一个新的
        Dim blk As AcadBlock = doc.Blocks.Add(New Double() {0, 0, 0}, "工件")
        blk.AddBox(New Double() {0.5, 0.5, 0.5}, 1, 1, 1)
        Return blk
    End Function


    Public Function DrawPlate(ByRef doc As AcadDocument, ByRef dr As DataRow) As AcadBlockReference Implements ICadPlateService.DrawPlate


        Dim px As Double = dr("px")
        Dim py As Double = dr("py")
        Dim pz As Double = dr("pz")

        Dim sx As Double = dr("sx")
        Dim sy As Double = dr("sy")
        Dim sz As Double = dr("sz")

        Dim rx As Double = dr("rx") * (Math.PI / 180)
        Dim ry As Double = dr("ry") * (Math.PI / 180)
        Dim rz As Double = dr("rz") * (Math.PI / 180)



        Dim b As AcadBlock = BuildBasePlateBlock(doc)
        Dim plate As AcadBlockReference = doc.ModelSpace.InsertBlock(New Double() {0, 0, 0}, b.Name, sx, sy, sz, 0)
        cadApplicationService.RotateEntity(plate, New Double() {rx, ry, rz})
        plate.InsertionPoint = New Double() {px, py, pz}
        Return plate
    End Function


End Class
